<template>
  <div class="white_div">
    <el-card>
      <!-- 增加用户 -->
      <el-button type="success" @click="addPerson" style="float: right"
        icon="el-icon-circle-plus-outline">增加用户</el-button>

      <el-dialog title="增加用户" :visible.sync="addDialogVisible" width="28vw">
        <el-form label-width="auto">
          <el-form-item label="电话:" width="22vw">
            <el-input v-model="addPersonItem.userTel" placeholder="请输入电话" width="20vw"></el-input>
          </el-form-item>
          <el-form-item label="靶场积分:">
            <el-input v-model="addPersonItem.userjifen1" placeholder="请输入靶场积分"></el-input>
          </el-form-item>
          <el-form-item label="ctf积分:">
            <el-input v-model="addPersonItem.userJifen2" placeholder="请输入ctf积分"></el-input>
          </el-form-item>
          <el-form-item label="注册时间:">
            <el-input v-model="addPersonItem.userTime" placeholder="请输入注册时间" type="date"></el-input>
          </el-form-item>
          <el-form-item label="注册地:">
            <el-input v-model="addPersonItem.userLocation" placeholder="请输入注册地"></el-input>
          </el-form-item>
          <el-form-item label="昵称:">
            <el-input v-model="addPersonItem.userName" placeholder="请输入昵称"></el-input>
          </el-form-item>
          <el-form-item label="资产:">
            <el-input v-model="addPersonItem.userProperty" placeholder="请输入资产"></el-input>
          </el-form-item>
          <el-form-item label="状态:">
            <el-select style="float: left" v-model="addPersonItem.userStatus">
              <el-option value="0" label="正常"></el-option>
              <el-option value="1" label="冻结"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户类型:">
            <el-select style="float: left" v-model="addPersonItem.userType">
              <el-option value="0" label="学生"></el-option>
              <el-option value="1" label="讲师"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="VIP">
            <el-select style="float: left" v-model="addPersonItem.userVip">
              <el-option value="0" label="普通"></el-option>
              <el-option value="1" label="会员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别:">
            <el-select style="float: left" v-model="addPersonItem.userSex">
              <el-option value="男" label="男"></el-option>
              <el-option value="女" label="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="头像">
            <!-- <el-upload
              style="float: left"
              class="avatar-uploader"
              action="http://172.16.13.26:8090/uploadoss"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="addPersonItem.userSrc"
                :src="addPersonItem.userSrc"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload> -->
            <el-upload class="upload-demo" action="http://172.16.13.212:8090/uploadoss" :on-remove="handleRemove"
              :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <el-button size="small" type="primary" class="push">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
            <el-image :src="addPersonItem.userSrc" alt=""></el-image>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addPersonApi">确定</el-button>
        </span>
      </el-dialog>

      <!-- 表格区域 -->
      <el-table :data="list" stripe border class="userinfo">
        <el-table-column prop="userId" label="编号" align="center" width="100px"></el-table-column>
        <el-table-column prop="userName" label="昵称" align="center"></el-table-column>
        <el-table-column prop="userType" label="用户类型" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.userType == '0' ? 'primary' : 'success'" v-if="scope.row.userType == '0'">
              学生
            </el-tag>
            <el-tag :type="scope.row.userType == '0' ? 'primary' : 'success'" v-if="scope.row.userType == '1'">
              教师
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="userjifen1" label="实验靶场积分" align="center" width="109.375vw">
          <template slot-scope="scope">
            <span v-if="scope.row.userjifen1 == null">暂无</span>
            <span v-if="scope.row.userjifen1 != null">{{ scope.row.userjifen1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userjifen2" label="ctf积分" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.userjifen2 == null">暂无</span>
            <span v-if="scope.row.userjifen2 != null">{{ scope.row.userjifen2 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userProperty" label="资产" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.userProperty == null">暂无</span>
            <span v-if="scope.row.userProperty != null">{{ scope.row.userProperty }}云币</span>
          </template>
        </el-table-column>
        <el-table-column prop="userSrc" label="头像" align="center">
          <template slot-scope="scope">
            <el-avatar :size="50" :src="scope.row.userSrc"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="updatePersons(scope)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deletePersons(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="changePageSize" @current-change="changeCurrentPage" :current-page="query.currentPage"
        :page-sizes="[5, 10, 15, 20]" :page-size="query.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改的模态框 -->
    <el-dialog title="修改用户信息" :visible.sync="updateDialogVisible" width="600px">
      <el-form label-width="auto">
        <el-form-item label="编号：">
          <el-input v-model="updatePersonItem.userId" placeholder="请输入编号" readonly></el-input>
        </el-form-item>
        <el-form-item label="电话：">
          <el-input v-model="updatePersonItem.userTel" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="靶场积分：">
          <el-input v-model="updatePersonItem.userjifen1" placeholder="请输入靶场积分" readonly></el-input>
        </el-form-item>
        <el-form-item label="ctf积分：">
          <el-input v-model="updatePersonItem.userJifen2" placeholder="请输入ctf积分" readonly></el-input>
        </el-form-item>
        <el-form-item label="注册时间：">
          <el-input v-model="updatePersonItem.userTime" placeholder="请输入注册时间" readonly></el-input>
        </el-form-item>
        <el-form-item label="注册地：">
          <el-input v-model="updatePersonItem.userLocation" placeholder="请输入注册地" readonly></el-input>
        </el-form-item>
        <el-form-item label="昵称：">
          <el-input v-model="updatePersonItem.userName" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="资产：">
          <el-input v-model="updatePersonItem.userProperty" placeholder="请输入资产" readonly></el-input>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select style="float: left" v-model="updatePersonItem.userStatus">
            <el-option value="0" label="正常"></el-option>
            <el-option value="1" label="冻结"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型：">
          <el-select style="float: left" v-model="updatePersonItem.userType">
            <el-option value="0" label="学生"></el-option>
            <el-option value="1" label="讲师"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="VIP：">
          <el-select style="float: left" v-model="updatePersonItem.userVip">
            <el-option value="0" label="普通"></el-option>
            <el-option value="1" label="会员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别：">
          <el-select style="float: left" v-model="updatePersonItem.userSex">
            <el-option value="男" label="男"></el-option>
            <el-option value="女" label="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像：">
          <el-upload style="float: left" class="avatar-uploader" action="http://172.16.13.212:8090/uploadoss"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="updatePersonItem.userSrc" :src="updatePersonItem.userSrc" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updatePersonApi">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
import dayjs from "dayjs";
import { createNamespacedHelpers } from "vuex";
const { mapMutations: mapTabsMutations } = createNamespacedHelpers("tabs");
const {  mapMutations } = createNamespacedHelpers("tags");
export default {
  data() {
    return {
      list: [],
      total: 0,
      query: {
        pageSize: 10,
        currentPage: 1,
      },
      addDialogVisible: false,
      updateDialogVisible: false,
      //修改编辑
      updatePersonItem: {
        userId: "",
        userTel: "",
        userjifen1: "",
        userJifen2: "",
        userTime: "",
        userLocation: "",
        userName: "",
        userPaw: "",
        userProperty: "",
        userStatus: "",
        userType: "",
        userSex: "",
        userSrc: "",
      },
      //增加信息
      addPersonItem: {
        // userId: "",
        userTel: "",
        userjifen1: "",
        userJifen2: "",
        userTime: "",
        userLocation: "",
        userName: "",
        userPaw: "",
        userProperty: "",
        userStatus: "",
        userType: "",
        userSex: "",
        userSrc: "",
      },
    };
  },
  methods: {
    ...mapMutations(["addTabs"]),
    ...mapTabsMutations(["addTabs"]),
    async addPerson() {
      this.addDialogVisible = true;
    },
    async getPerson() {
      console.log("aaaaaaa", this.query);
      const result = await this.$api.person.allPerson(this.query);
      console.log("===result===", result);
      this.list = result.data.userList;
      this.total = result.data.total;
    },
    changeCurrentPage(args) {
      console.log("args", args);
      this.query.currentPage = args;
      this.getPerson();
    },
    changePageSize(args) {
      console.log("args2", args);
      this.query.pageSize = args;
      this.getPerson();
    },
    //修改编辑信息
    async updatePersons(args) {
      console.log("args", args.row);
      this.updatePersonItem = { ...args.row };
      const userId = args.row.userId;
      const result = await this.$api.person.searchId(userId);
      console.log("***result***", result);
      //显示模态框
      this.updateDialogVisible = true;
    },
    //修改编辑之后
    async updatePersonApi() {
      console.log("修改后的数据", this.updatePersonItem);
      const result = await this.$api.person.updatePerson(this.updatePersonItem);
      console.log("---results---", result);
      if (result.code) {
        this.$message.success(result.message);
        this.getPerson();
        //设置时间格式化
        this.updatePersonItem.userTime = new Date();
        this.updatePersonItem.userTime = dayjs(
          this.updatePersonItem.userTime
        ).format("YYYY/MM/DD");
        //模态框关闭
        this.updateDialogVisible = false;
      }
    },

    //增加用户
    async addPersonApi() {
      const result = await this.$api.person.addPerson(this.addPersonItem);
      console.log("164", result);
      console.log("results", result.result);
      if (result.code) {
        this.$message.success(result.message);
        //将表单中的数据清空
        this.$message.success(result.message);
        this.addPersonItem.userId = "";
        this.addPersonItem.userTel = "";
        this.addPersonItem.userjifen1 = "";
        this.addPersonItem.userJifen2 = "";
        this.addPersonItem.userTime = "";
        this.addPersonItem.userLocation = "";
        this.addPersonItem.userPaw = "";
        this.addPersonItem.userName = "";
        this.addPersonItem.userProperty = "";
        this.addPersonItem.userType = "";
        this.addPersonItem.userStatus = "";
        this.addPersonItem.userSex = "";
        this.addPersonItem.userSrc = "";
       
        //模态框关闭
        this.addDialogVisible = false;
      }
    },
    //删除按钮
    async deletePersons(args) {
      console.log("userId---", typeof Number(args.userId));
      const result = await this.$api.person.deletePerson(Number(args.userId));

      console.log("result222", result);
      if (result.code) {
        this.$message.success(result.message);
        //模态框关闭
        this.getPerson();
      }
    },
    //增加
    // handleAvatarSuccess(args) {
    //   console.log("args11111", args);
    //   // this.addPersonItem.image = `http://172.16.13.26:8090${args.message}`;
    //   this.addPersonItem.image = args.message;
    //   console.log(this.addPersonItem.image);
    // },
    // beforeAvatarUpload(file) {
    //   const isJPG = file.type === "image/jpeg";
    //   const isLt2M = file.size / 1024 / 1024 < 2;

    //   if (!isJPG) {
    //     this.$message.error("上传头像图片只能是 JPG 格式!");
    //   }
    //   if (!isLt2M) {
    //     this.$message.error("上传头像图片大小不能超过 2MB!");
    //   }
    //   return isJPG && isLt2M;
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleAvatarSuccess(val) {
      console.log("图片", val.message);
      this.addPersonItem.userSrc = val.message;
      this.updatePersonItem.userSrc = val.message;
    },
    beforeAvatarUpload(file) {
      const isJPGaPNG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPGaPNG) {
        this.$message.error("上传头像图片只能是 JPG或PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      console.log("aaaaa", isLt2M);
      return isLt2M && isJPGaPNG;
    },
  },
  created() {
    this.getPerson();
  },
  beforeRouteEnter(to,from,next){
        next(vm=>{
           vm.addTabs(to)
        })
 },
};
</script>
  
<style lang="scss" scoped>
.el-form-item__label-wrap {
  float: left;
  margin-left: 0px !important;
}

.avatar-uploader ::v-deep .el-upload {
  border: 0.052vw dashed #d9d9d9;
  border-radius: 0.313vw;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 1.458vw;
  color: #8c939d;
  width: 9.271vw;
  height: 9.271vw;
  line-height: 9.271vw;
  text-align: center;
}

.avatar {
  width: 9.271vw;
  height: 9.271vw;
  display: block;
}

// .userinfo {
//   width: 54vw;
// }
</style>